﻿using UnityEngine;
using UnityEngine.EventSystems;
using LuaInterface;

public class ButtonAnimation : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    public delegate void VoidDelegate(GameObject go);
    public LuaFunction onDown;
    public LuaFunction onUp;
    
    static public ButtonAnimation Get(GameObject go)
    {
        ButtonAnimation listener = go.GetComponent<ButtonAnimation>();
        if (listener == null) listener = go.AddComponent<ButtonAnimation>();
        return listener;
    }

    public static void AddDown(GameObject go, LuaFunction func)
    {
        ButtonAnimation listner = Get(go);
        listner.onDown = func;
    }

    public static void AddUp(GameObject go, LuaFunction func)
    {
        ButtonAnimation listner = Get(go);
        listner.onUp = func;
    }

    public static void AddDown(Transform go, LuaFunction func)
    {
        AddDown(go.gameObject, func);
    }

    public static void AddUp(Transform go, LuaFunction func)
    {
        AddUp(go.gameObject, func);
    }

   
    public void OnPointerDown(PointerEventData eventData)
    {
        if (onDown != null) onDown.Call(eventData);
    }
    public void OnPointerUp(PointerEventData eventData)
    {
        if (onUp != null) onUp.Call(eventData);
    }
}
